﻿package com.accp.crm.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import com.accp.crm.entity.BasDict;
import com.accp.crm.entity.CstActivity;
import com.accp.crm.entity.CstCustomer;
import com.accp.crm.entity.CstLinkman;
import com.accp.crm.entity.PageBean;
import com.accp.crm.entity.SysUser;
import com.accp.crm.service.IBasdictService;
import com.accp.crm.service.ICstActivity;
import com.accp.crm.service.ICstCustomerService;
import com.accp.crm.service.ICstLinkman;
import com.accp.crm.service.IUserService;

/**
 * 用户表 Action Bean
 * @author hailong.liu
 */
public class CustomerAction extends DispatchAction {
	
	 private ICstCustomerService custservice;
	    
	 public void setCustomerService(ICstCustomerService custservice){
	    this.custservice=custservice;
	 }
	 private IBasdictService basservice;

		public void setBasdictService(IBasdictService basservice) {
			this.basservice = basservice;
		}
		
		private IUserService userservice;
		
		public void setUserSevice(IUserService userservice){
			this.userservice=userservice;
		}
		
	    private ICstLinkman cstlinkmanService;
		
		public void setCstLinkman(ICstLinkman cstlinkmanService){
			this.cstlinkmanService = cstlinkmanService;
		}
		private ICstActivity cstActivityservice;
		
		public void setCstActivity(ICstActivity cstActivityservice){
			this.cstActivityservice=cstActivityservice;
		}

		public ActionForward toAdd(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			List list=this.basservice.findByDqHql("地区");
			request.setAttribute("DICTS_OPTIONS_Area", list);
			List liststep = this.basservice.findByKhHql("客户等级");
			request.setAttribute("DICTS_OPTIONS_Step", liststep);
			List listuser = this.userservice.findAll();
			request.setAttribute("USERS_OPTIONS", listuser);
			return mapping.findForward("addpage");
		}
	 
	 /**
		 * 分页查询
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return
		 */
		public ActionForward dolist(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			DynaActionForm custform=(DynaActionForm) form;
			CstCustomer cust=(CstCustomer) custform.get("cust");
			int num=0;
			String pageNo=request.getParameter("pageNo");
			if(pageNo==null||pageNo.isEmpty()){
				num=0;
			}
			else{
				num=Integer.parseInt(pageNo);
				if(num<0){
					num=0;
				}
			}
			
			PageBean page=this.custservice.findByStatus(num,cust.getCustNo(),cust.getCustManagerName());
			request.setAttribute("custList",page.getList());
			request.setAttribute("page", page);
			custform.initialize(mapping);
			return mapping.findForward("listPage");
		}
		
		public ActionForward doAdd(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			DynaActionForm custform=(DynaActionForm) form;
			CstCustomer cust = (CstCustomer) custform.get("cust");
			this.custservice.add(cust);
			return mapping.findForward("dolist");
		}
		
		public ActionForward preUpdate(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			DynaActionForm custform = (DynaActionForm) form;
			String id = request.getParameter("id");
			CstCustomer cust = this.custservice.findById(id);
			custform.set("cust", cust);
			List list=this.basservice.findByDqHql("地区");
			request.setAttribute("DICTS_OPTIONS_Area", list);
			List liststep = this.basservice.findByKhHql("客户等级");
			request.setAttribute("DICTS_OPTIONS_Step", liststep);
			List listuser = this.userservice.findAll();
			request.setAttribute("USERS_OPTIONS", listuser);
			request.setAttribute("cust", cust);
			return mapping.findForward("updatePage");
		}
		
		public ActionForward doUpdate(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			DynaActionForm custform = (DynaActionForm) form;
			CstCustomer cust = (CstCustomer) custform.get("cust");
			SysUser user=this.userservice.findByid(cust.getCustManagerId());
			cust.setCustManagerName(user.getUsrName());
			String level=cust.getCustLevel().toString();
			BasDict bas=this.basservice.findByHql(level);
			cust.setCustLevelLabel(bas.getDictItem());
			this.custservice.update(cust);
			custform.initialize(mapping);
			return mapping.findForward("dolist");
		}
		
		
		public ActionForward doDelete(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			String id = request.getParameter("id");
			this.custservice.delete(Integer.parseInt(id));
			return mapping.findForward("dolist");
		}

		

		public ActionForward tolinkman(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			String id=request.getParameter("id");
			CstCustomer cust=this.custservice.findById(id);
			request.setAttribute("cust", cust);
			List linkmanlist= this.cstlinkmanService.findAll(cust.getCustNo());
			request.setAttribute("linkmanList", linkmanlist);
			return mapping.findForward("doLinkman");
		}

		public ActionForward toRegister(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			String id=request.getParameter("id");
			CstCustomer cust=this.custservice.findById(id);
			request.setAttribute("cust", cust);
			List activitylist = this.cstActivityservice.findAll(cust.getCustNo());
			request.setAttribute("activityList", activitylist);
			return mapping.findForward("doRegister");
		}
		
		

		
}
